package com.zhsource.system.controller;

import com.github.pagehelper.PageInfo;
import com.zhsource.auth.PermissionScanAnno;
import com.zhsource.exception.GlobalException;
import com.zhsource.system.domain.Role;
import com.zhsource.system.query.RoleQuery;
import com.zhsource.system.service.IRoleService;
import com.zhsource.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>@ClassName：RoleController
 * <p>@Date 2023/10/17  10:21
 *
 * @Author zzl
 **/
@Slf4j
@Api(tags = "角色接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    // 查询所有

    @ApiOperation("查询所有")
    @GetMapping
    public R<List<Role>> list() {
        try {
            return R.ok("查询所有成功!", roleService.list());
        } catch (GlobalException e) {
            log.error("Role模块的查询所有发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Role模块的查询所有发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }
    // 查询单个

    @GetMapping("/{id}")
    @ApiOperation(value = "根据Id获取角色信息")
    @PermissionScanAnno( name = "根据Id查询" , sn = "role:get")
    public R<Role> selectById(@ApiParam(value = "角色ID",readOnly = true) @PathVariable("id") Long id) {
        try {
            return R.ok("查询单个成功!", roleService.selectById(id));
        } catch (GlobalException e) {
            log.error("Role模块的查询单个发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Role模块的查询单个发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    // 分页+ 模糊查询
    @PostMapping("/page")
    @PermissionScanAnno( name = "分页展示角色" , sn = "role:page")
    public R<PageInfo<Role>> page(@RequestBody RoleQuery roleQuery ) {
        try {
            return R.ok("分页查询成功!", roleService.page(roleQuery));
        } catch (GlobalException e) {
            log.error("Role模块的分页查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Role模块的分页查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }


    // 修改
    @PutMapping()
    @PermissionScanAnno( name = "修改角色" , sn = "role:insert")
    public R<Void> update(@RequestBody Role role ) {
        try {
            roleService.update(role);
            return R.ok("修改数据成功!");
        } catch (GlobalException e) {
            log.error("Role模块的修改发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Role模块的修改发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    // 新增

    @PostMapping()
    @PermissionScanAnno( name = "新增角色" , sn = "role:update")
    public R<Void> insert(@RequestBody Role role ) {
        try {
            roleService.insert(role);
            return R.ok("新增数据成功!");
        } catch (GlobalException e) {
            log.error("Role模块的新增发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Role模块的新增发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }
    // 删除单个

    @DeleteMapping("/{id}")
    @PermissionScanAnno( name = "删除角色" , sn = "role:delete")
    public R<Void> delete(@PathVariable("id") Long id) {
        try {
            roleService.delete(id);
            return R.ok("删除单个成功!");
        } catch (GlobalException e) {
            log.error("Role模块的删除单个发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Role模块的查询删除发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    // 批量删除
    @PatchMapping("/batchDel")
    @PermissionScanAnno( name = "批量删除角色" , sn = "role:batchDel")
    public R<Void> batchDel(@RequestBody List<Long> ids) {
        try {
            roleService.batchDel(ids);
            return R.ok("批量删除成功!");
        } catch (GlobalException e) {
            log.error("Role模块的批量删除发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Role模块的批量删除发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

}
